package main;

import Bean.UserInfoBean;
import utils.DBUtils;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.jar.JarEntry;

public class User {

    static DBUtils dbUtils;
    static Scanner sc = new Scanner(System.in);
    public static void mainScreen(UserInfoBean user){
        System.out.println("\n\n\n");
        System.out.println("---------这里是用户界面---------");
        System.out.println("欢迎 用户 （ " + user.getName() +" ) ~~~~~~~~~~~  您的账户有 :" + user.getAsset() + "    ########### 银行卡号是：" +user.getBankCard());
        userBreak: while(true) {
            System.out.println("请选择你要做什么:");
            System.out.println("1 - 存钱 ");
            System.out.println("2 - 取钱 ");
            System.out.println("3 - 转账");
            System.out.println("4 - 管理信息");
            System.out.println("5 - 退出登录");

            switch (sc.nextInt()) {
                case 1:
                    save(user);
                    break userBreak;
                case 2:
                    withDraw(user);
                    break userBreak;
                case 3:
                    transfer(user);
                    break userBreak;
                case 4:
                    UserManage.manageUser(user);
                    break userBreak;
                case 5:
                    new Open().start();
                    break userBreak;
                default:
                    System.out.println("不要胡乱输入哦！！！");
                    break;
            }
        }
    }

    public static void save(UserInfoBean user){
        String saveMoney = moneyNotZero();
        BigDecimal sm = new BigDecimal(saveMoney);

        for(int i = 0 ; i < 3 ; i ++) {
            System.out.println("请输入支付密码:");
            String pw = sc.next();

            if(pw.equals(user.getPwd())){
                sm = sm.add(user.getAsset());
                dbUtils.updateAsset(sm , user.getID());
                break;
            }
            System.out.println("密码错误，你还有 " + (3 - i - 1) +" 次机会");
        }
        System.out.println("存放操作成功！");
        user.setAsset(sm);
        mainScreen(user);
    }

    public static void withDraw(UserInfoBean user){
        String wdMoney = moneyNotZero();
        BigDecimal wm = new BigDecimal(wdMoney);
        if(wm.compareTo(user.getAsset()) < 0) {
            for (int i = 0; i < 3; i++) {
                System.out.println("请输入支付密码:");
                String pw = sc.next();

                if (pw.equals(user.getPwd())) {
                    wm = user.getAsset().subtract(wm);
                    dbUtils.updateAsset(wm, user.getID());
                    break;
                }
                System.out.println("密码错误，你还有 " + (3 - i - 1) + " 次机会");
            }
        }else{
            System.out.println("您的资产不够进行转账");
            mainScreen(user);
        }
        System.out.println("取出操作成功！");
        user.setAsset(wm);
        mainScreen(user);
    }

    public static void transfer(UserInfoBean user){
        System.out.println("请输入要转账的银行卡号：");
        String card = sc.next();
        String sql = "select * from userinfo where `Del` = 0 and BankCard = \'" + card +"\' ";
        try{
            UserInfoBean userTo = dbUtils.selectUser(sql);
            if(userTo != null){
                String wdMoney = moneyNotZero();
                BigDecimal sm = new BigDecimal(wdMoney);

                if(user.getAsset().compareTo(sm) < 0){
                    System.out.println("你没有足够的资金可以进行转账！！！");
                    mainScreen(user);
                    return;
                }

                for(int i = 0 ; i < 3 ; i ++) {
                    System.out.println("请输入支付密码:");
                    String pw = sc.next();
                    if(pw.equals(user.getPwd())){
                        // 向目标加sm的资金
                        dbUtils.updateAsset(sm.add(userTo.getAsset()) , userTo.getID());
                        // 将user减去sm的资金
                        dbUtils.updateAsset(user.getAsset().subtract(sm) , user.getID());
                        break;
                    }
                    System.out.println("密码错误，你还有 " + (3 - i - 1) +" 次机会");
                }
                System.out.println("成功转账！！！");
                user.setAsset(user.getAsset().subtract(sm));
                mainScreen(user);
            }else{
                System.out.println("该用户未开户，或银行卡输入错误！！");
                mainScreen(user);
            }
        }catch (SQLException e){
            System.out.println("数据库没有此用户");
        }
    }


    public static String moneyNotZero(){
        String money;
        moneybreak :while(true) {
            System.out.println("请输入要取出的金额：");
            money = sc.next();
            if (Double.parseDouble(money) > 0) {
                break moneybreak;
            }else{
                System.out.println("金额不能为负数");
            }
        }
        return money;
    }

}
